package com.fbs.wowo.manage.mapper;

import com.fbs.wowo.manage.entity.Permission;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;
import java.util.Set;

public interface PermissionMapper {
    @Delete({
        "delete from permission",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer id);

    @Insert({
        "insert into permission (id, name, ",
        "description)",
        "values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, ",
        "#{description,jdbcType=VARCHAR})"
    })
    int insert(Permission record);

    @InsertProvider(type=PermissionSqlProvider.class, method="insertSelective")
    int insertSelective(Permission record);

    @Select({
        "select",
        "id, name, description",
        "from permission",
        "where id = #{id,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="description", property="description", jdbcType=JdbcType.VARCHAR)
    })
    Permission selectByPrimaryKey(Integer id);

    @UpdateProvider(type=PermissionSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(Permission record);

    @Update({
        "update permission",
        "set name = #{name,jdbcType=VARCHAR},",
          "description = #{description,jdbcType=VARCHAR}",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(Permission record);

    /**
     * 按角色查找权限
     * @param id 角色 主键id
     * @return Permission list
     */
    @Select({
            "select",
            "p.id, p.name, p.description",
            "from permission p",
            "inner join role_permission r",
            "on p.id = r.permission_id",
            "where r.role_id = #{id,jdbcType=INTEGER}"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
            @Result(column="description", property="description", jdbcType=JdbcType.VARCHAR)
    })
    List<Permission> findByRoleId(Integer id);

    /**
     * 按用户id查找权限字符串集合
     * @param id
     * @return permission string set
     */
    @Select({
            "select",
            "p.name",
            "from permission p",
            "inner join user_role u",
            "inner join role_permission r",
            "on r.role_id = u.role_id and p.id=r.permission_id",
            "where u.user_id = #{userId,jdbcType=VARCHAR}"
    })
    Set<String> findPermissionStr(String userId);

    /**
     * 插入权限角色表
     * @param roleId    角色id
     * @param permissionId 权限id
     */
    @Insert({
            "insert into role_permission (role_id, permission_id)",
            "values (#{roleId,jdbcType=INTEGER}, #{permissionId,jdbcType=INTEGER})"
    })
    void addPermissionToRole(@Param("roleId") Integer roleId, @Param("permissionId") Integer permissionId);
}